/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { registerAs } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import * as path from 'path';

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
export const databaseConfig = registerAs(
  'database',
  (): TypeOrmModuleOptions => ({
    type: process.env.DB_TYPE as 'mysql',
    host: process.env.DB_HOST,
    port: parseInt(process.env.DB_PORT as string, 10) || 3306,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE,
    charset: process.env.DB_CHARSET,
    // 自动加载实体
    entities: [path.join(__dirname, '../**/*.entity{.ts,.js}')],

    // 生产环境应为 false
    synchronize: process.env.DB_SYNCHRONIZE === 'true',
    logging: process.env.DB_LOGGING === 'true',

    // 连接池配置
    extra: {
      connectionLimit: 10,
    },

    // 其它 TypeORM 配置
    migrations: [path.join(__dirname, '../migrations/*{.ts,.js}')],
    migrationsRun: false,
  }),
);
